package com.asiainfo.complier.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;

import com.asiainfo.complier.action.JdkSettingAction;
import com.asiainfo.complier.conf.SysSetting;
import com.asiainfo.complier.helper.ArrayHelper;
import com.asiainfo.complier.helper.StringHelper;
import com.asiainfo.complier.helper.UIHelper;

public class JavaSettingDlg extends JDialog {

	/**
	 * JavaSettingDlg
	 */
	private static final long serialVersionUID = -5439121258466678676L;

	private JList jarListBox;
	private JList customerjarListBox;
	private JComboBox jdkComboBox;
	private JTextField outputPath;

	private JCheckBox lineBox;
	private JCheckBox painterlineBox;
	private JCheckBox caretBox;

	private ArrayList<String> customerList;

	public JavaSettingDlg(JFrame parent) {
		super(parent);
		customerList = new ArrayList<String>();
		initUI();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	/**
	 * doModal
	 */
	public void doModal() {
		setTitle(UIHelper.getResource("lan_prefences"));
		setSize(660, 300);
		setResizable(false);
		UIHelper.centerWindow(this);
		setVisible(true);
		setModal(true);
	}

	/**
	 * saveEditorStatus
	 */
	private void saveEditorStatus() {
		SysSetting.SHOW_LINE = lineBox.isSelected() ? "true" : "false";
		SysSetting.HIGHTER_LINE = painterlineBox.isSelected() ? "true"
				: "false";
		SysSetting.HIGHTER_CARET = caretBox.isSelected() ? "true" : "false";
		//
		SysSetting.CUSTOMER_CLASS_PATH = ArrayHelper.toArraySplit(customerList);
	}

	/**
	 * initJavaSetting
	 */
	private void initJavaSetting() {
		SysSetting.initSetting();

		//
		if (SysSetting.SHOW_LINE.equals("true"))
			lineBox.setSelected(true);
		else {
			lineBox.setSelected(false);
		}
		if (SysSetting.HIGHTER_LINE.equals("true"))
			painterlineBox.setSelected(true);
		else {
			painterlineBox.setSelected(false);
		}
		if (SysSetting.HIGHTER_CARET.equals("true"))
			caretBox.setSelected(true);
		else {
			caretBox.setSelected(false);
		}

		outputPath.setText(SysSetting.OUTPUT_PATH);

		// jdk home
		DefaultComboBoxModel jdkModel = new DefaultComboBoxModel();
		String jdkVersion = SysSetting.JAVA_VERSION;
		jdkModel.addElement(jdkVersion);
		SysSetting.JAVA_VERSION = jdkVersion;
		jdkComboBox.setModel(jdkModel);

		// classpath

		DefaultComboBoxModel model = new DefaultComboBoxModel();
		String jdk_clasPath = SysSetting.JDK_CLASS_PATH;
		if (!StringHelper.isNullOrEmpty(jdk_clasPath)) {
			String clasArr[] = jdk_clasPath.split(";");

			for (String s : clasArr) {
				model.addElement(s);
			}

			jarListBox.setModel(model);
		}
		// customer classpath
		String customer_clasPath = SysSetting.CUSTOMER_CLASS_PATH;
		DefaultComboBoxModel customer_model = new DefaultComboBoxModel();
		if (!StringHelper.isNullOrEmpty(customer_clasPath)) {
			String clasArr[] = customer_clasPath.split(";");

			for (String s : clasArr) {
				customerList.add(s);
				customer_model.addElement(s);
			}

		}
		customerjarListBox.setModel(customer_model);

	}

	private JPanel javaSettingPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		JPanel centeJPanel = new JPanel();
		JPanel rightJPanel = new JPanel();
		centeJPanel.setPreferredSize(new Dimension(276, -1));
		panel.setBorder(new TitledBorder(UIHelper.getResource("lan_java_setting")));
		rightJPanel.setPreferredSize(new Dimension(180, -1));
		JLabel empyJLabel = new JLabel();
		empyJLabel.setPreferredSize(new Dimension(180, 12));
		JButton btRequired = new JButton(UIHelper.getResource("lan_require_jar"));
		btRequired.setToolTipText(UIHelper.getResource("lan_require_jar"));
		final ImageIcon icon = UIHelper
				.getImageIco("com/asiainfo/images/archive.png");
		btRequired.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser(".");
				chooser.setMultiSelectionEnabled(true);
				chooser.setFileView(new FileView() {
					public Icon getIcon(File f) {
						if (f.isFile()
								&& f.getAbsolutePath().toLowerCase()
										.endsWith(".jar"))
							return icon;
						return null;
					}

				});
				chooser.setFileFilter(new FileFilter() {

					@Override
					public String getDescription() {

						return "jar File|*.jar";
					}

					@Override
					public boolean accept(File f) {

						return f.isDirectory()
								|| (f.isFile() && f.getAbsolutePath()
										.toLowerCase().endsWith(".jar"));
					}
				});

				int ret = chooser.showOpenDialog(JavaSettingDlg.this);
				if (JFileChooser.APPROVE_OPTION == ret) {
					DefaultComboBoxModel model = (DefaultComboBoxModel) customerjarListBox
							.getModel();
					File[] files = chooser.getSelectedFiles();
					for (File f : files) {
						if (!customerList.contains(f.getAbsolutePath())) {
							customerList.add(f.getAbsolutePath());
							model.addElement(f.getAbsolutePath());

						}
					}

					customerjarListBox.setModel(model);
					SysSetting.CUSTOMER_CLASS_PATH = ArrayHelper
							.toArraySplit(customerList);

				}
			}

		});

		JButton btChooseJava = new JButton(UIHelper.getResource("lan_choose_jdk"));
		btChooseJava.setToolTipText(UIHelper.getResource("lan_choose_jdk"));
		btChooseJava.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int ret = chooser.showOpenDialog(JavaSettingDlg.this);
				if (JFileChooser.APPROVE_OPTION == ret) {
					String path = chooser.getSelectedFile().getAbsolutePath();
					JdkSettingAction jdkAction = new JdkSettingAction(path);
					try {

						@SuppressWarnings("unchecked")
						ArrayList<String> jarList = (ArrayList<String>) jdkAction
								.doExecute();
						SysSetting.JDK_PATH = jdkAction.getJavaHome();
						DefaultComboBoxModel model = new DefaultComboBoxModel();
						String classPath = "";
						for (String s : jarList) {
							model.addElement(s);
							classPath += s + ";";
						}
						classPath += ".";
						jarListBox.setModel(model);
						SysSetting.JDK_CLASS_PATH = classPath;
						DefaultComboBoxModel jdkModel = new DefaultComboBoxModel();
						String jdkVersion = jdkAction.getJavaVersion();
						jdkModel.addElement(jdkVersion);
						SysSetting.JAVA_VERSION = jdkVersion;
						jdkComboBox.setModel(jdkModel);

					} catch (Exception ex) {

						JOptionPane.showMessageDialog(JavaSettingDlg.this,
								ex.getMessage());
						return;
					}
				}

			}
		});
		JButton btJavaDel = new JButton(UIHelper.getResource("lan_remove_jdk"));
		btJavaDel.setToolTipText(UIHelper.getResource("lan_remove_jdk"));
		JButton btDelJAR = new JButton(UIHelper.getResource("lan_remove_require_jar"));
		btDelJAR.setToolTipText(UIHelper.getResource("lan_remove_require_jar"));
		btDelJAR.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Object[] objList = customerjarListBox.getSelectedValues();
				DefaultComboBoxModel model = (DefaultComboBoxModel) customerjarListBox
						.getModel();
				for (Object object : objList) {
					model.removeElement(object);
					customerList.remove(object);
				}

			}
		});

		btChooseJava.setPreferredSize(new Dimension(170, 26));
		btJavaDel.setPreferredSize(new Dimension(170, 26));
		btRequired.setPreferredSize(new Dimension(170, 26));
		btDelJAR.setPreferredSize(new Dimension(170, 26));

		rightJPanel.add(empyJLabel);
		rightJPanel.add(btChooseJava);
		rightJPanel.add(btJavaDel);
		rightJPanel.add(btRequired);
		rightJPanel.add(btDelJAR);

		JPanel contentPanel = new JPanel();
		JPanel topPanel = new JPanel();

		topPanel.setLayout(new BorderLayout());
		contentPanel.setLayout(new BorderLayout());
		JTabbedPane jarJTabbedPane = new JTabbedPane();

		JScrollPane jarJarScrollPane = new JScrollPane();
		JScrollPane customerJarScrollPane = new JScrollPane();

		jarJTabbedPane.addTab(UIHelper.getResource("lan_java_setting"), jarJarScrollPane);
		jarJTabbedPane.addTab(UIHelper.getResource("lan_setting_jar"), customerJarScrollPane);

		jarListBox = new JList();
		jarJarScrollPane.getViewport().add(jarListBox);
		jarListBox.setCellRenderer(new DefaultCellRenderer());

		customerjarListBox = new JList();
		customerjarListBox.setCellRenderer(new DefaultCellRenderer());
		customerJarScrollPane.getViewport().add(customerjarListBox);

		centeJPanel.setLayout(new BorderLayout());
		JLabel lbJdkJLabel = new JLabel(UIHelper.getResource("lan_jdk_version"));
		jdkComboBox = new JComboBox();
		topPanel.add(lbJdkJLabel, BorderLayout.WEST);
		topPanel.add(jdkComboBox, BorderLayout.CENTER);

		contentPanel.add(topPanel, BorderLayout.SOUTH);
		contentPanel.add(jarJTabbedPane, BorderLayout.CENTER);

		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new BorderLayout(2, 2));

		JLabel outputJLabel = new JLabel(UIHelper.getResource("lan_out_out_path"));
		outputPath = new JTextField(" ");
		JButton ouptBt = new JButton("...");
		ouptBt.setToolTipText(UIHelper.getResource("lan_out_out_path"));
		ouptBt.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser(".");
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int ret = chooser.showOpenDialog(JavaSettingDlg.this);
				if (JFileChooser.APPROVE_OPTION == ret) {
					SysSetting.OUTPUT_PATH = chooser.getSelectedFile()
							.getAbsolutePath();
					outputPath.setText(chooser.getSelectedFile()
							.getAbsolutePath());
				}
			}

		});
		bottomPanel.add(outputJLabel, BorderLayout.WEST);
		bottomPanel.add(outputPath, BorderLayout.CENTER);
		bottomPanel.add(ouptBt, BorderLayout.EAST);

		rightJPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		JPanel titleJPanel = new JPanel();
		titleJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));

		centeJPanel.add(bottomPanel, BorderLayout.SOUTH);
		centeJPanel.add(topPanel, BorderLayout.NORTH);
		centeJPanel.add(contentPanel, BorderLayout.CENTER);

		this.setLayout(new BorderLayout());

		panel.add(centeJPanel, BorderLayout.CENTER);
		panel.add(rightJPanel, BorderLayout.EAST);
		return panel;
	}

	private JPanel javaEditorPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		JPanel centeJPanel = new JPanel();
		centeJPanel.setPreferredSize(new Dimension(276, -1));
		centeJPanel.setLayout(new BorderLayout());

		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
		bottomPanel.setBorder(new TitledBorder(UIHelper.getResource("lan_java_setting")));
		lineBox = new JCheckBox(UIHelper.getResource("lan_show_line"));

		bottomPanel.add(lineBox);
		painterlineBox = new JCheckBox(UIHelper.getResource("lan_show_highterline"));

		bottomPanel.add(painterlineBox);
		caretBox = new JCheckBox(UIHelper.getResource("lan_show_care_pair"));
		bottomPanel.add(caretBox);
		centeJPanel.add(bottomPanel, BorderLayout.CENTER);

		this.setLayout(new BorderLayout());

		panel.add(centeJPanel, BorderLayout.CENTER);

		return panel;
	}

	private void initUI() {
		JPanel centerPanel = new JPanel();
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		JPanel btPanel = new JPanel();
		JPanel javasPane = javaSettingPanel();
		JPanel editorPane = javaEditorPanel();
		this.setLayout(new BorderLayout());
		centerPanel.setLayout(new BorderLayout());
		tabbedPane.addTab(UIHelper.getResource("lan_java_setting"), javasPane);
		tabbedPane.addTab(UIHelper.getResource("lan_editor_setting"), editorPane);
		centerPanel.add(tabbedPane);
		JButton btApply = new JButton(UIHelper.getResource("lan_apply"));
		btApply.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				try {
					saveEditorStatus();
					SysSetting.saveSetting();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		});
		JButton btSure = new JButton(UIHelper.getResource("lan_ok"));

		btSure.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				saveEditorStatus();
				SysSetting.saveSetting();
				setModal(false);
				dispose();
			}

		});
		JButton btExit = new JButton(UIHelper.getResource("lan_exit"));
		btExit.setMnemonic(KeyEvent.VK_ESCAPE);
		btExit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				setModal(false);
				dispose();
			}
		});
		btPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 5));

		btPanel.add(btApply);
		btPanel.add(btSure);
		btPanel.add(btExit);
		this.getContentPane().add(centerPanel, BorderLayout.CENTER);
		this.getContentPane().add(btPanel, BorderLayout.SOUTH);

		initJavaSetting();

	}
}
